package com.hk.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hk.Enume.OperationType;
import com.hk.annotation.AotoFile;
import com.hk.constant.LogConstaant;
import com.hk.core.PageResult;
import com.hk.core.Result;
import com.hk.domain.Dict;
import com.hk.domain.DictData;
import com.hk.dto.DictListDto;
import com.hk.service.DictDataService;
import com.hk.service.DictService;
import com.hk.utils.JwtUtils;
import com.hk.vo.DictDataVo;
import com.hk.vo.DictVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/dict")
@RequiredArgsConstructor
@Slf4j
public class DictController {


    private final DictService dictService;
    private final DictDataService dictDataService;
    /*
    * 新增字典
    * */
@PostMapping("/type")
@AotoFile(value = OperationType.POST,title = "字典管理")
public Result save(@RequestBody DictVo dictVo){
    dictService.saveDict(dictVo);
    return Result.success();
}
@GetMapping("/data/type/{dictType}")
public Result getData(@PathVariable String dictType){
    List<DictData> list=dictDataService.list(Wrappers.<DictData>lambdaQuery().eq(DictData::getDictType,dictType));
    return Result.success(list);

}
@GetMapping("/type/list")
public PageResult dictList(DictListDto dictListDto){
    PageResult pageResult=dictService.dictlist(dictListDto);

    return pageResult;
}
@GetMapping("/type/{dictId}")
public Result getByTypeId(@PathVariable Integer dictId){
    Dict dict=dictService.getOne(Wrappers.<Dict>lambdaQuery().eq(Dict::getDictId,dictId));
    return Result.success(dict);
}
@GetMapping("/data/list")
public PageResult dictDataList(Integer pageNum,Integer pageSize,String dictType){
    PageResult pageResult=dictDataService.dictDatalist(pageNum,pageSize,dictType);
    return pageResult;
}


@DeleteMapping("/type/{ids}")
@AotoFile(value = OperationType.DELETE,title = LogConstaant.DICT_LOG)
public Result delete(@PathVariable List<Integer> ids){
    dictService.delectDict(ids);
    return Result.success();
}


@PutMapping("/type")
@AotoFile(value = OperationType.PUT,title = LogConstaant.DICT_LOG)
public Result update(@RequestBody DictVo dictVo){
    dictService.updateDict(dictVo);
    return Result.success();
}


@PostMapping("/data")
@AotoFile(value = OperationType.POST,title = LogConstaant.DICT_LOG)
public Result saveData(@RequestBody DictData dictData){
    dictDataService.save(dictData);
    return Result.success();
}

@GetMapping("/data/{dictCode}")
public Result updateData(@PathVariable Integer dictCode){
    DictData byId = dictDataService.getById(dictCode);
    return Result.success(byId);
}


@PutMapping("/data")
@AotoFile(value = OperationType.PUT,title = LogConstaant.DICT_LOG)
public Result updateData(@RequestBody DictData dictData){
    dictDataService.updateById(dictData);
    return Result.success();
}


@DeleteMapping("/data/{ids}")
@AotoFile(value = OperationType.DELETE,title = LogConstaant.DICT_LOG)
public Result deleteData(@PathVariable List<Integer> ids){
    dictDataService.removeBatchByIds(ids);
    return Result.success();
}

}
